/*
 * ParametersWindow.java
 *
 * Created on 4 mars 2009, 21:34:18
 */
package ncorps.gui;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import ncorps.commun.NCorpsConsole;
import ncorps.commun.StatNewtonFourier;
import ncorps.output.ChartStatOutFrame;
import ncorps.output.ConsoleFrame;
import ncorps.output.ScreenOutFrame;

/**
 *
 * @author Gilles
 */
public class ParametersWindow extends javax.swing.JFrame {

    private NCorpsGui MainBoard;
    private String RepImages;
    private String RepFiles;
    private String RepStat;
    private boolean affichestat;
    private ConsoleFrame OutPutFrame;
    private NCorpsConsole ncorpsconsole;

    public ParametersWindow() {
        initComponents();
    }

    public JTextArea getSortietext() {
        return OutPutFrame.getSortietext();
    }

    public JTextField getTimefield() {
        return OutPutFrame.getTimefield();
    }

    public boolean isAffichestat() {
        return affichestat;
    }

    public ParametersWindow(NCorpsGui aThis) {
        this.MainBoard = aThis;
        new ParametersWindow().setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainpanel = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        tempsFinal = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        DT = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        _N = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        rayon = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        checkfourier = new javax.swing.JCheckBox();
        checkfichiersfourier = new javax.swing.JCheckBox();
        checkimagesfourier = new javax.swing.JCheckBox();
        visionneuseF = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        checknewton = new javax.swing.JCheckBox();
        checkfichiersnewton = new javax.swing.JCheckBox();
        checkimagesnewton = new javax.swing.JCheckBox();
        visionneuseN = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        repimages = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        repfiles = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        repstat = new javax.swing.JTextField();
        start = new javax.swing.JButton();
        statjbutton = new javax.swing.JButton();
        clean = new javax.swing.JButton();
        closebutton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("PARAMETRES");
        setAlwaysOnTop(true);
        setBackground(java.awt.Color.white);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);
        getContentPane().setLayout(new java.awt.BorderLayout(5, 5));

        mainpanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(java.awt.Color.gray, java.awt.Color.blue));
        mainpanel.setLayout(new java.awt.GridLayout(32, 1, 2, 2));

        jLabel7.setFont(new java.awt.Font("Tahoma", 2, 12)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Paramètres");
        mainpanel.add(jLabel7);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setForeground(java.awt.Color.blue);
        jLabel1.setText("    Temps Final");
        mainpanel.add(jLabel1);

        tempsFinal.setFont(new java.awt.Font("Arial", 1, 12));
        tempsFinal.setText("100");
        mainpanel.add(tempsFinal);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setForeground(java.awt.Color.blue);
        jLabel2.setText("    deltaT");
        mainpanel.add(jLabel2);

        DT.setFont(new java.awt.Font("Arial", 1, 12));
        DT.setText("0.005");
        mainpanel.add(DT);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setForeground(java.awt.Color.blue);
        jLabel3.setText("    Nb coprs");
        mainpanel.add(jLabel3);

        _N.setFont(new java.awt.Font("Arial", 1, 12));
        _N.setText("200");
        mainpanel.add(_N);

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel12.setForeground(java.awt.Color.blue);
        jLabel12.setText("    Rayon de la grille");
        mainpanel.add(jLabel12);

        rayon.setFont(new java.awt.Font("Arial", 1, 12));
        rayon.setText("32");
        mainpanel.add(rayon);

        jLabel4.setBackground(new java.awt.Color(0, 0, 0));
        mainpanel.add(jLabel4);

        checkfourier.setFont(new java.awt.Font("Tahoma", 3, 11));
        checkfourier.setForeground(java.awt.Color.blue);
        checkfourier.setSelected(true);
        checkfourier.setText("              FOURIER");
        mainpanel.add(checkfourier);

        checkfichiersfourier.setFont(new java.awt.Font("Tahoma", 1, 11));
        checkfichiersfourier.setForeground(java.awt.Color.blue);
        checkfichiersfourier.setSelected(true);
        checkfichiersfourier.setText("fichiers");
        mainpanel.add(checkfichiersfourier);

        checkimagesfourier.setFont(new java.awt.Font("Tahoma", 1, 11));
        checkimagesfourier.setForeground(java.awt.Color.blue);
        checkimagesfourier.setSelected(true);
        checkimagesfourier.setText("Images");
        mainpanel.add(checkimagesfourier);

        visionneuseF.setFont(new java.awt.Font("Arial", 3, 14));
        visionneuseF.setForeground(java.awt.Color.red);
        visionneuseF.setText("VISIONNEUSE");
        visionneuseF.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        visionneuseF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                visionneuseFActionPerformed(evt);
            }
        });
        mainpanel.add(visionneuseF);
        mainpanel.add(jLabel5);

        checknewton.setFont(new java.awt.Font("Tahoma", 3, 11));
        checknewton.setForeground(java.awt.Color.blue);
        checknewton.setSelected(true);
        checknewton.setText("               NEWTON");
        mainpanel.add(checknewton);

        checkfichiersnewton.setFont(new java.awt.Font("Tahoma", 1, 11));
        checkfichiersnewton.setForeground(java.awt.Color.blue);
        checkfichiersnewton.setSelected(true);
        checkfichiersnewton.setText("fichiers");
        mainpanel.add(checkfichiersnewton);

        checkimagesnewton.setFont(new java.awt.Font("Tahoma", 1, 11));
        checkimagesnewton.setForeground(java.awt.Color.blue);
        checkimagesnewton.setSelected(true);
        checkimagesnewton.setText("Images");
        mainpanel.add(checkimagesnewton);

        visionneuseN.setFont(new java.awt.Font("Arial", 3, 14));
        visionneuseN.setForeground(java.awt.Color.red);
        visionneuseN.setText("VISIONNEUSE");
        visionneuseN.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        visionneuseN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                visionneuseNActionPerformed(evt);
            }
        });
        mainpanel.add(visionneuseN);
        mainpanel.add(jLabel6);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel8.setForeground(java.awt.Color.blue);
        jLabel8.setText("Repertoire des images");
        mainpanel.add(jLabel8);

        repimages.setFont(new java.awt.Font("Tahoma", 1, 12));
        repimages.setForeground(java.awt.Color.darkGray);
        repimages.setText("D:\\NetbeansProjects\\NBodies2D\\IMG\\");
            mainpanel.add(repimages);

            jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12));
            jLabel9.setForeground(java.awt.Color.blue);
            jLabel9.setText("Repertoire des fichiers");
            mainpanel.add(jLabel9);

            repfiles.setFont(new java.awt.Font("Tahoma", 1, 12));
            repfiles.setForeground(java.awt.Color.darkGray);
            repfiles.setText("D:\\NetbeansProjects\\NBodies2D\\FILE\\");
                mainpanel.add(repfiles);

                jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12));
                jLabel10.setForeground(java.awt.Color.blue);
                jLabel10.setText("Repertoire des fichies statistique");
                mainpanel.add(jLabel10);

                repstat.setFont(new java.awt.Font("Tahoma", 1, 12));
                repstat.setForeground(java.awt.Color.darkGray);
                repstat.setText("D:\\NetbeansProjects\\NBodies2D\\STAT\\");
                    mainpanel.add(repstat);

                    start.setFont(new java.awt.Font("Arial", 1, 14));
                    start.setText("START");
                    start.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
                    start.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            startActionPerformed(evt);
                        }
                    });
                    mainpanel.add(start);

                    statjbutton.setFont(new java.awt.Font("Arial", 1, 14));
                    statjbutton.setForeground(java.awt.Color.blue);
                    statjbutton.setText("Analyse");
                    statjbutton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            statjbuttonActionPerformed(evt);
                        }
                    });
                    mainpanel.add(statjbutton);

                    clean.setFont(new java.awt.Font("Arial", 3, 12)); // NOI18N
                    clean.setForeground(java.awt.Color.gray);
                    clean.setText("nettoyer les répertoires");
                    clean.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            cleanActionPerformed(evt);
                        }
                    });
                    mainpanel.add(clean);

                    closebutton.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
                    closebutton.setText("Sauvegarder et Fermer");
                    closebutton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            closebuttonActionPerformed(evt);
                        }
                    });
                    mainpanel.add(closebutton);

                    getContentPane().add(mainpanel, java.awt.BorderLayout.CENTER);

                    setBounds(10, 120, 290, 721);
                }// </editor-fold>//GEN-END:initComponents

    private void closebuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closebuttonActionPerformed
        new SaveAndClose().setVisible(true);
        //        this.dispose();
}//GEN-LAST:event_closebuttonActionPerformed

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        start.setForeground(Color.RED);
        OutPutFrame = new ConsoleFrame();
        OutPutFrame.setVisible(true);

        RepImages = repimages.getText();
        new File(RepImages).mkdir();
        RepFiles = repfiles.getText();
        new File(RepFiles).mkdir();
        RepStat = repstat.getText();
        new File(RepStat).mkdir();
        //

        int tempsMax = Integer.parseInt(tempsFinal.getText());
        double dt = Double.parseDouble(DT.getText());
        int NbCorps = Integer.parseInt(_N.getText());
        int _R = Integer.parseInt(rayon.getText());
        boolean fourier = checkfourier.isSelected();
        boolean fourierimages = checkimagesfourier.isSelected();
        boolean fourierfichiers = checkfichiersfourier.isSelected();
        boolean newton = checknewton.isSelected();
        boolean newtonimages = checkimagesnewton.isSelected();
        boolean newtonfichiers = checkfichiersnewton.isSelected();

        try {
            ncorpsconsole = new NCorpsConsole(this, tempsMax, dt, NbCorps, _R, fourier, fourierimages, fourierfichiers, newton, newtonimages, newtonfichiers, RepImages, RepFiles, RepStat);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ParametersWindow.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ParametersWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        start.setForeground(Color.GREEN);
}//GEN-LAST:event_startActionPerformed

    private void cleanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cleanActionPerformed
        RepImages = repimages.getText();
        deleteDir(new File(RepImages));
        RepFiles = repfiles.getText();
        deleteDir(new File(RepFiles));
        RepStat = repstat.getText();
        deleteDir(new File(RepStat));
    }//GEN-LAST:event_cleanActionPerformed

    private void visionneuseFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_visionneuseFActionPerformed
        visonneuse("ImgF");
}//GEN-LAST:event_visionneuseFActionPerformed

    private void visionneuseNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_visionneuseNActionPerformed
        visonneuse("ImgN");
}//GEN-LAST:event_visionneuseNActionPerformed

    private void statjbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statjbuttonActionPerformed
        StatNewtonFourier stat = new StatNewtonFourier(ncorpsconsole.getThread());
        new ChartStatOutFrame(stat);
}//GEN-LAST:event_statjbuttonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField DT;
    private javax.swing.JTextField _N;
    private javax.swing.JCheckBox checkfichiersfourier;
    private javax.swing.JCheckBox checkfichiersnewton;
    private javax.swing.JCheckBox checkfourier;
    private javax.swing.JCheckBox checkimagesfourier;
    private javax.swing.JCheckBox checkimagesnewton;
    private javax.swing.JCheckBox checknewton;
    private javax.swing.JButton clean;
    private javax.swing.JButton closebutton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel mainpanel;
    private javax.swing.JTextField rayon;
    private javax.swing.JTextField repfiles;
    private javax.swing.JTextField repimages;
    private javax.swing.JTextField repstat;
    private javax.swing.JButton start;
    private javax.swing.JButton statjbutton;
    private javax.swing.JTextField tempsFinal;
    private javax.swing.JButton visionneuseF;
    private javax.swing.JButton visionneuseN;
    // End of variables declaration//GEN-END:variables

    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // The directory is now empty so delete it
        return dir.delete();
    }

    private void visonneuse(String imagenametype) {
        int tempsMax = 100;
//        String imagepath = "D:/NetbeansProjects/NBodies2D/IMG/";
//        String imagenametype = "ImgN";
        String format = "bmp";
        ScreenOutFrame I = new ScreenOutFrame();

        I.sortieEcran(RepImages, imagenametype, format, tempsMax);
        I.setVisible(true);


    }
}
